package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.quality.entity.*;
import com.mxpio.erp.common.quality.service.*;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Tag(name = "InQualityReportController", description = "质量大屏")
@RestController
@RequestMapping("/erp/quality/report/")
public class InQualityReportController {

    @Autowired
    private VEInspectionBillPassRateService veInspectionBillPassRateService;
    @Autowired
    private VEInspectionBillInspectionTasksService veInspectionBillInspectionTasksService;
    @Autowired
    private VEInspectionBillCompleteInspectionDayService veInspectionBillCompleteInspectionDayService;
    @Autowired
    private VEInspectionBillSupplierPassRateService veInspectionBillSupplierPassRateService;
    @Autowired
    private VEInspectionBillAssemblyProblemService veInspectionBillAssemblyProblemService;
    @Autowired
    private VEInspectionBillUnqualifiedPartsService veInspectionBillUnqualifiedPartsService;
    @Autowired
    private VEInspectionBillAssembleBadBackService veInspectionBillAssembleBadBackService;
    @Autowired
    private VEInspectionBillProcessMaterialScrapService veInspectionBillProcessMaterialScrapService;
    @Autowired
    private VEUnqualifiedDisposalService veUnqualifiedDisposalService;

    @GetMapping("pass/rate/list")
    @Operation(summary = "机加车间、装配车间、采购订单的合格率", description = "机加车间、装配车间、采购订单的合格率", method = "GET")
    public Result<List<VEInspectionBillPassRate>> getPassRate(Criteria criteria)  {
        List<VEInspectionBillPassRate> passRate = veInspectionBillPassRateService.getPassRate(criteria);
        for (VEInspectionBillPassRate rate : passRate) {
            rate.setPassRate(rate.getReceiveQuantity().divide(rate.getQuantity(),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
        }
        return Result.OK(passRate);
    }

    @GetMapping("inspection/tasks/list")
    @Operation(summary = "待检任务报表-不分页 ", description = "待检任务报表-不分页 ", method = "GET")
    public Result<List<VEInspectionBillInspectionTasks>> recordList(Criteria criteria) throws UnsupportedEncodingException {
        List<VEInspectionBillInspectionTasks> list = veInspectionBillInspectionTasksService.list(criteria);
        return Result.OK(list);
    }

    @GetMapping("inspection/tasks/page")
    @Operation(summary = "待检任务报表-分页 ", description = "待检任务报表-分页 ", method = "GET")
    public Result<Page<VEInspectionBillInspectionTasks>> recordPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<VEInspectionBillInspectionTasks> page = veInspectionBillInspectionTasksService.listPage(pageAble, criteria);
        return Result.OK(page);
    }

    @GetMapping("inspection/day/list")
    @Operation(summary = "当日完成检验-不分页 ", description = "当日完成检验-不分页 ", method = "GET")
    public Result<List<VEInspectionBillCompleteInspectionDay>> dayList(Criteria criteria) throws UnsupportedEncodingException {
        List<VEInspectionBillCompleteInspectionDay> list = veInspectionBillCompleteInspectionDayService.list(criteria);
        return Result.OK(list);
    }

    @GetMapping("inspection/day/page")
    @Operation(summary = "当日完成检验-分页 ", description = "当日完成检验-分页 ", method = "GET")
    public Result<Page<VEInspectionBillCompleteInspectionDay>> dayPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<VEInspectionBillCompleteInspectionDay> page = veInspectionBillCompleteInspectionDayService.listPage(pageAble, criteria);
        return Result.OK(page);
    }

    @GetMapping("supplier/pass/rate/list")
    @Operation(summary = "供应商合格率最低TOP5", description = "供应商合格率最低TOP5", method = "GET")
    public Result<List<VEInspectionBillSupplierPassRate>> getSupplierPassRate(Criteria criteria)  {
        List<VEInspectionBillSupplierPassRate> passRate = veInspectionBillSupplierPassRateService.getPassRate(criteria);
        for (VEInspectionBillSupplierPassRate rate : passRate) {
            rate.setPassRate(rate.getReceiveQuantity().divide(rate.getQuantity(),2,BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
        }
        // 确保列表不为null
        if (passRate != null && passRate.size()>0) {
            // 使用Stream API来获取passRate最小的5条数据
            List<VEInspectionBillSupplierPassRate> top5LowestPassRates = passRate.stream()
//                    .filter(rate -> rate.getPassRate().signum() > 0) // 过滤出通过率大于0的项
                    .sorted(Comparator.comparing(VEInspectionBillSupplierPassRate::getPassRate)) // 根据BigDecimal的compareTo方法进行排序
                    .limit(5)
                    .collect(Collectors.toList());
            return Result.OK(top5LowestPassRates);
        }
       // passRate，这里可以返回null或者一个空列表，根据你的需求来定
        return Result.OK(passRate);
    }

    @GetMapping("assembly/problem/list")
    @Operation(summary = "装配问题数量TOP10", description = "装配问题数量TOP10", method = "GET")
    public Result<List<VEInspectionBillAssemblyProblem>> getTOP10(Criteria criteria)  {
        List<VEInspectionBillAssemblyProblem> list = veInspectionBillAssemblyProblemService.getTOP10(criteria);
        return Result.OK(list);
    }

    @GetMapping("unqualified/parts/list")
    @Operation(summary = "采购件不合格-不分页 ", description = "采购件不合格-不分页 ", method = "GET")
    public Result<List<VEInspectionBillUnqualifiedParts>> partList(Criteria criteria) throws UnsupportedEncodingException {
        List<VEInspectionBillUnqualifiedParts> list = veInspectionBillUnqualifiedPartsService.list(criteria);
        return Result.OK(list);
    }

    @GetMapping("unqualified/parts/page")
    @Operation(summary = "采购件不合格-分页 ", description = "采购件不合格-分页 ", method = "GET")
    public Result<Page<VEInspectionBillUnqualifiedParts>> partPage( Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Criteria criteria = new Criteria();
        Page<VEInspectionBillUnqualifiedParts> page = veInspectionBillUnqualifiedPartsService.listPage(pageAble, criteria);
        return Result.OK(page);
    }

    @GetMapping("unqualified/parts")
    @Operation(summary = "采购件不合格-饼图", description = "采购件不合格-饼图", method = "GET")
    public Result<?> getParts()  {
        return veInspectionBillUnqualifiedPartsService.getlist();
    }

    @GetMapping("bad/back/list")
    @Operation(summary = "装配不良退料占比-不分页 ", description = "装配不良退料占比-不分页 ", method = "GET")
    public Result<List<VEInspectionBillAssembleBadBack>> badList(Criteria criteria) throws UnsupportedEncodingException {
        List<VEInspectionBillAssembleBadBack> list = veInspectionBillAssembleBadBackService.list(criteria);
        return Result.OK(list);
    }

    @GetMapping("bad/back/page")
    @Operation(summary = "装配不良退料占比-分页 ", description = "装配不良退料占比-分页 ", method = "GET")
    public Result<Page<VEInspectionBillAssembleBadBack>> badPage( Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<VEInspectionBillAssembleBadBack> page = veInspectionBillAssembleBadBackService.listPage(pageAble);
        return Result.OK(page);
    }

    @GetMapping("bad/back")
    @Operation(summary = "装配不良退料占比-饼图", description = "装配不良退料占比-饼图", method = "GET")
    public Result<?>getbad(Criteria criteria)  {
        return veInspectionBillAssembleBadBackService.getlist(criteria);
//        return Result.OK(passRate);
    }


//    @GetMapping("parts/scrap")
//    @Operation(summary = "零件报废", description = "零件报废", method = "GET")
//    public Result<List<VEInspectionBillProcessMaterialScrap>>getParts(Criteria criteria)  {
//        return veInspectionBillAssembleBadBackService.getlist(criteria);
////        return Result.OK(passRate);
//    }

    @GetMapping("parts/scrap/list")
    @Operation(summary = "零件报废-不分页 ", description = "零件报废-不分页 ", method = "GET")
    public Result<List<VEInspectionBillProcessMaterialScrap>> partsList() throws UnsupportedEncodingException {
        LocalDate today = LocalDate.now(); // 获取当前日期
        String year =Integer.toString(today.getYear()); // 获取年份
        Criteria criteria = Criteria.create().addCriterion("year", Operator.EQ, year);
        List<VEInspectionBillProcessMaterialScrap> list = veInspectionBillProcessMaterialScrapService.list(criteria);
        for (VEInspectionBillProcessMaterialScrap scrap : list) {
            if (scrap.getProcessNum() == null) {
                scrap.setProcessNum("0");
            }
            if (scrap.getFgNum() == null) {
                scrap.setFgNum("0");
            }
        }
        return Result.OK(list);
    }

    @GetMapping("parts/scrap/page")
    @Operation(summary = "零件报废-分页 ", description = "零件报废-分页 ", method = "GET")
    public Result<Page<VEInspectionBillProcessMaterialScrap>> partsPage(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        LocalDate today = LocalDate.now(); // 获取当前日期
        String year =Integer.toString(today.getYear()); // 获取年份
        criteria.addCriterion("year", Operator.EQ, year);
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<VEInspectionBillProcessMaterialScrap> page = veInspectionBillProcessMaterialScrapService.listPage(pageAble, criteria);
        return Result.OK(page);
    }

    @GetMapping("unqualified/page")
    @Operation(summary = "不良品处理展示报表-分页 ", description = "不良品处理展示报表-分页 ", method = "GET")
    public Result<Page<VEUnqualifiedDisposal>> getUnqualifiedDisposal(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
        Page<VEUnqualifiedDisposal> page = veUnqualifiedDisposalService.listPage(pageAble, criteria);
        return Result.OK(page);
    }

}
